<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程代码" prop="courseCode">
        <el-input
          v-model="queryParams.courseCode"
          placeholder="请输入课程代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="queryParams.courseType" placeholder="请选择课程类型" clearable>
          <el-option
            v-for="dict in dict.type.course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程状态" prop="courseStatus">
        <el-select v-model="queryParams.courseStatus" placeholder="请选择课程状态" clearable>
          <el-option
            v-for="dict in dict.type.course_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['schedule:schedule:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['schedule:schedule:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['schedule:schedule:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['schedule:schedule:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="courseId" />-->
      <el-table-column label="课程名称" align="center" prop="courseName"/>
      <el-table-column label="课程代码" align="center" prop="courseCode"/>
      <el-table-column label="学分 " align="center" prop="credits"/>
      <!--      <el-table-column label="部门id" align="center" prop="deptId" />-->
      <!--      <el-table-column label="教师ID" align="center" prop="teacherId" />-->
      <el-table-column label="上课时间" align="center" prop="classTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.classTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上课地点" align="center" prop="classLocation"/>
      <el-table-column label="开课学期" align="center" prop="semester"/>
      <!--      <el-table-column label="课程描述" align="center" prop="courseDescription" />-->
      <el-table-column label="课程容量(人数)" align="center" prop="courseCapacity"/>
      <el-table-column label="课程类型" align="center" prop="courseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_type" :value="scope.row.courseType"/>
        </template>
      </el-table-column>
      <el-table-column label="课程状态" align="center" prop="courseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_status" :value="scope.row.courseStatus"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="addStudent(scope.row)"-->
          <!--          >添加课程学生-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="selectStudent(scope.row)"
          >查看课程学生
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="skipClassTests(scope.row)"
          >随堂测试信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['schedule:schedule:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['schedule:schedule:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称"/>
        </el-form-item>
        <el-form-item label="课程代码" prop="courseCode">
          <el-input v-model="form.courseCode" placeholder="请输入课程代码"/>
        </el-form-item>
        <el-form-item label="学分 " prop="credits">
          <el-input-number v-model="form.credits" placeholder="请输入学分 "/>
        </el-form-item>
        <el-form-item label="上课时间" prop="classTime">
          <el-date-picker clearable
                          v-model="form.classTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择上课时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上课地点" prop="classLocation">
          <el-input v-model="form.classLocation" placeholder="请输入上课地点"/>
        </el-form-item>
        <el-form-item label="开课学期" prop="semester">
          <el-input v-model="form.semester" placeholder="请输入开课学期"/>
        </el-form-item>
        <el-form-item label="课程描述" prop="courseDescription">
          <el-input type="textarea" v-model="form.courseDescription" placeholder="请输入课程描述"/>
        </el-form-item>
        <el-form-item label="课程容量(人数)" prop="courseCapacity">
          <el-input-number v-model="form.courseCapacity" placeholder="请输入课程容量(人数)"/>
        </el-form-item>
        <el-form-item label="课程类型" prop="courseType">
          <el-select v-model="form.courseType" placeholder="请选择课程类型">
            <el-option
              v-for="dict in dict.type.course_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程状态" prop="courseStatus">
          <el-select v-model="form.courseStatus" placeholder="请选择课程状态">
            <el-option
              v-for="dict in dict.type.course_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看课程学生列表 -->
    <el-dialog title="课程学生列表" :visible.sync="open2" width="800px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="addStudent()"
          >新增学生
          </el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading2" :data="studentList">
        <el-table-column label="头像" align="center" prop="avatar" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="学生名" align="center" prop="nickName"/>
        <el-table-column label="手机号" align="center" prop="phonenumber"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="deleteStudent(scope.row.userId)"
            >删除
            </el-button>
          </template>
        </el-table-column>
        <div slot="footer" class="dialog-footer">
          <!--          <el-button type="primary" @click="submitForm">确 定</el-button>-->
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </el-table>

      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="pageNum2"
        :limit.sync="pageSize2"
        @pagination="getList2"
      />
    </el-dialog>

    <!-- 查看课程学生列表 -->
    <el-dialog title="添加学生" :visible.sync="open3" width="600px" append-to-body>
      <el-select v-model="studentUserId" placeholder="请选择学生" clearable>
        <el-option
          v-for="dict in studentPullDown"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
      </el-select>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm3">确 定</el-button>
        <el-button @click="close3">关 闭</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import {
  listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule,
  selectStudentUserByDeptId, addStudent, studentListByCourseId, deleteStudent
} from "@/api/schedule/schedule";

export default {
  name: "Schedule",
  dicts: ['course_type', 'course_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      // 课程管理表格数据
      scheduleList: [],
      //课程学生数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      pageNum2: 1,
      pageSize2: 10,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseCode: null,
        deptId: null,
        teacherId: null,
        courseType: null,
        courseStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          {required: true, message: "课程名称不能为空", trigger: "blur"}
        ],
        courseCode: [
          {required: true, message: "课程代码不能为空", trigger: "blur"}
        ],
      },
      courseId: null,
      studentUserId: null,
      //学生下拉数据
      studentPullDown: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除课程学生
    deleteStudent(userId) {
      deleteStudent({courseId: this.courseId, studentUserId: userId}).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList2(this.courseId);
      });
    },
    selectStudent(row) {
      this.courseId = row.courseId;
      this.getList2(row.courseId);
      this.open2 = true;
    },

    addStudent() {
      selectStudentUserByDeptId().then(response => {
        this.studentPullDown = response.data;
        this.open3 = true;
      });
    },
    skipClassTests(row) {
      this.$router.push({path: "/teacher/classTests", query: {courseId: row.courseId}});
    },
    /** 查询课程管理列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询课程学生列表 */
    getList2(courseId) {
      this.loading2 = true;
      studentListByCourseId({courseId: courseId}).then(response => {
        this.studentList = response.rows;
        this.total2 = response.total;
        this.loading2 = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 关闭按钮
    close() {
      this.open2 = false;
    },
    // 关闭按钮
    close3() {
      this.open3 = false;
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        courseCode: null,
        credits: null,
        deptId: null,
        teacherId: null,
        classTime: null,
        classLocation: null,
        semester: null,
        courseDescription: null,
        courseCapacity: null,
        courseType: null,
        courseStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getSchedule(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程管理";
      });
    },
    /** 添加学生 */
    submitForm3() {
      addStudent({courseId: this.courseId, studentUserId: this.studentUserId}).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open3 = false;
        this.getList2(this.courseId);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除课程管理编号为"' + courseIds + '"的数据项？').then(function () {
        return delSchedule(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('schedule/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
